<?php
namespace app\servergoods\service\goods;
use \app\manage\service\ApiBase;

/**
 * @author:      李颖
 * @DateTime:    2017-05-26 10:16:55
 * @Description: 商品分类属性
 * 验证：
 		添加或编辑时：
 			[不需要验证的字段:''默认值]   [需要验证但不require的:' '默认,使其验证]   [必须require的字段:无需默认值]
 		搜索时：
 			可空，给予''默认值，跳过验证器即搜索值为空时，不验证，不搜索
 			可以为空 也可以不传  为空或不传都不进行搜索
 			设置了且存在的情况下才会进行搜索
 * 编辑类，传什么验什么
 */

class GoodsCategoryAttrs extends ApiBase
{
	protected $goodsCategoryAttrsLogic = 'app\servergoods\logic\GoodsCategoryAttrs';
	protected $goodsCategoryAttrsValidate = 'app\servergoods\validate\GoodsCategoryAttrs';
	protected $addFields = [
		'name'=>' ','en_name'=>' ','attr_type'=>' ','attr_values'=>'','default_value'=>'',
		'is_filter'=>' ','is_show'=>' ','show_position'=>' ','search_flag'=>' ','order'=>100
	];
	protected $searchFields = [
		'name'=>'','attr_type'=>''
	];
	/*获取属性列表(分页)*/
	public function attrsList(): array
	{
		$data = $this->receiveData(['page'=>1,'listRows'=>15]);
		$search = $this->issetReceiveData($this->searchFields);

		$validate = validate($this->goodsCategoryAttrsValidate);
        if(!$validate->check($search)){
            return verify_err(null,$validate->getError());
        }

		$data = array_merge($data,$search);

		return $this->logic($this->goodsCategoryAttrsLogic)->attrsList($data);
	}
	/*获取属性列表不分页*/
	public function attrsAllList(): array 
	{
		$data = $this->issetReceiveData($this->searchFields);
		return $this->stepRun($data, 'attrsAllList', 'attrsAllList');
	}
	/*获取某属性信息*/
	public function oneAttrInfo(): array
	{
		$data = $this->issetReceiveData(['id'=>0, 'en_name'=>' ']);
		if(!isset($data['id']) && !isset($data['en_name'])){
			return verify_err(null, 'id和en_name必须传一个');
		}
        return $this->stepRun($data,'oneAttrInfo','oneAttrInfo');
	}

	/*添加属性*/
	public function addAttr(): array
	{
        $data = $this->receiveData($this->addFields);
        return $this->stepRun($data,'addAttr');
	}

	/*编辑属性*/
	public function editAttr(): array
	{
		$data = $this->issetReceiveData(array_merge($this->addFields,['id'=>0]));
        return $this->stepRun($data,'editAttr');
	}

	//删除一个或多个属性
    public function delAttrs(): array
	{
        $data = $this->receiveData(['id'=>[]]);
        return $this->stepRun($data,'delAttrs','checkIdArray');
    }

    /*根据en_name获取多个属性详情*/
    public function getSomeAttrs(): array 
    {
    	$data = $this->haveIssetReceiveData(['en_name'=>' ']);
    	if(is_string($data['en_name'])){
    		$data['en_name'] = explode(",", $data['en_name']);
    	}elseif(!is_array($data['en_name'])){
    		return verify_err(null, 'en_name必须为字符串或数组');
    	}
    	return $this->stepRun($data, 'getSomeAttrs', 'getSomeAttrs');
    }
}
